package com.group18.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.group18.db.DBUtil;
import com.group18.entity.FamilyUser;


/**
 * User dao类
 * @author Nicky
 *
 */
public class FamilyUserDao extends BaseDao{
	
	/**
	 * 模糊搜索
	 * @param name
	 * @return
	 */
	public List<FamilyUser> SelectFamilyByName(String name) {
		List<FamilyUser> list = new ArrayList<FamilyUser>();
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select * from family where name like ?");
			preparedStatement.setString(1, "%"+name+"%");
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				FamilyUser u = new FamilyUser();
				u.setId(resultSet.getInt("id"));
				u.setName(resultSet.getString("name"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closed();
		}
		return list;
	}
	
	/**
	 * 根据id查询家庭
	 * @param u
	 * @return
	 */
	public String selectNameByID(FamilyUser u) {
		String name = "";
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select name from family where id = ?");
			preparedStatement.setInt(1, u.getId());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				name = resultSet.getString("name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}	
		
		return name;
	}
	
	/**
	 * 查询家庭所属id
	 * @param u
	 * @return	所查家庭的id
	 */
	public int selectIDByName(FamilyUser u) {
		
		int family_id = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select id from family where name = ?");
			preparedStatement.setString(1, u.getName());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				family_id = resultSet.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}	
		return family_id;
	}
	
	/**
	 * 添加家庭
	 * @param u
	 * @return	返回修改数据库条数0 失败 1成功
	 */
	public int addFamilyUser(FamilyUser u) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "insert into family(name) values(?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getName());
			count = preparedStatement.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 修改家庭
	 * @param u
	 * @return	修改数据条数
	 */
	public int upDataFamily(FamilyUser u) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("update family set name = ? where id = ?");
			preparedStatement.setString(1, u.getName());
			preparedStatement.setInt(2, u.getId());
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * 判断家庭名对应的数据是否存在
	 * @param u
	 * @return	true 存在，false 不存在
	 */
	public boolean isExist(FamilyUser u) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from family where name = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getName());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return flag;
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	public List<FamilyUser> selectAllUser() {
		
		List<FamilyUser> uList = new ArrayList<>();
		
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from family";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()) {
				FamilyUser u = new FamilyUser();
				u.setId(resultSet.getInt("id"));
				u.setName(resultSet.getString("name"));
				uList.add(u);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return uList;
	}
	
	/**
	 * 删除数据
	 * @param id
	 * 				要删除的id
	 * @return
	 * 				删除数据的条数
	 */
	public int deleteByID(int id) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("delete from family where id = ?");
			preparedStatement.setInt(1, id);
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return i;
	}
	
}
